﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using STHPMS1.Common.C_Extension;
using STHPMS1.Web.W_Model;
using System.Collections.Generic;
using Newtonsoft.Json;
using STHPMS1.Common.C_Utils;
using System.Data;
using System.Reflection;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/S_Inspeaccse")]
    public class S_InspeaccseController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetS_InspeaccseByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            var data = this.Scene.Bll.S_Inspeaccse.Get<S_Inspeaccse>(null, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        /// <summary>
        /// 返回道路名称
        /// </summary>
        /// <returns></returns>
        [Route("RoadWay")]
        public IHttpActionResult GetRoadWay()
        {
            return Ok(this.Scene.Bll.B_Roadway.GetAll<B_Roadway>());
        }
        [Route("Access")]
        public HttpResponseMessage PostAccess(string inspecid)
        {
            try
            {
                this.Scene.Bll.E_Roadwaydetail.Insert(inspecid);
                //更改评定的抗滑列
                this.Scene.Bll.S_Inspeaccse.UpdateStatus(inspecid, "ISACCESS");
            }
            catch(Exception ex)
            {
                throw ex;
            }
            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetS_InspeaccseByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("")]
        public HttpResponseMessage PostSave(string inspecid, string rdid,string y)
        {
            //查询该评定编号是否存在
            if (this.Scene.Bll.S_Inspeaccse.Exists(inspecid))
            {
                //存在抛出错误
                var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("评定ID已经存在"),
                    ReasonPhrase = "Server Error"
                };
                throw new HttpResponseException(response);
            }
            else
            {
                var newInspc = new S_Inspeaccse()
                {
                    Inspecid = inspecid,
                    Issub = false,
                    Isrddf = false,
                    Isbtstrut = false,
                    Isrutting = false,
                    Isrdpssi = false,
                    Isrdfrc = false,
                    Isrdrgh = false,
                    Isasphalt = false,
                    Iscement = false,
                    Isgravel = false,
                    Isaccess = false,
                     Year = Convert.ToInt32( y)
                };
                this.Scene.Bll.S_Inspeaccse.Save(newInspc);
                var response = Request.CreateResponse(HttpStatusCode.Created);
                // Generate a link to the new book and set the Location header in the response.
                string uri = Url.Link("GetS_InspeaccseByPage", new { page = 1 });
                response.Headers.Location = new Uri(uri);

                return response;
            }
        }

        /// <summary>
        /// 上传数据处理
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="rdid">路线编号</param>
        /// <param name="dir">调查方向</param>
        /// <param name="v">评定ID和导入类型</param>
        /// <returns></returns>
        [Route("saveFile")]
        public HttpResponseMessage PostSaveFile(string file, string rdid, string dir, string v)
        {
            string[] temp = v.Split('|');
            string insid = temp[0];//评定ID
            string type = temp[1];//导入类型
            DataTable entity = null;//实体数据
            Excel_BaseImportData exData = null;//导入数据
            Dictionary<string,List<string>> old = new Dictionary<string,List<string>>();//旧数据
            if (File.Exists(file))
            {
                try
                {
                    //获取路线下指定方向的所有单元,进行模糊匹配
                    List<B_Unit> b_unitList = this.Scene.Bll.B_Unit.GetUnitList<B_Unit>(dir, rdid);

                    int startRow = 0;//Excel列头位置
                    if(type=="kh")
                    {
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetKHData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Rdfrc());//抗滑实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Rdfrc"; //抗滑 
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的抗滑列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISRDFRC");
                    }
                    else if(type=="pzd")
                    {
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetPZDData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Rdrgh());//平整度实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Rdrgh"; //平整度 
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的平整度列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISRDRGH");
                    }
                    else if(type=="cz")
                    {
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetCZData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Rutting());//车辙实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Rutting"; //车辙 
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的车辙列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISRUTTING");
                    }
                    else if(type=="qd")
                    {
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetQDData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Rdpssi());//强度实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Rdpssi"; //强度
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的强度列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISRDPSSI");
                    }
                    else if(type=="gzw")
                    {
                        startRow = 1;
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetGZWData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Btstrut());//构造物实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Btstrut"; //构造物
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的强度列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISBTSTRUT");
                    }
                    else if (type == "ss")
                    {
                        startRow = 1;
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetSSData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Rddf());//设施实体
                        exData.DtSource = entity;
                        exData.TableName = "S_RDDF"; //设施
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的强度列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISRDDF");
                    }
                    else if (type == "lq")
                    {
                        startRow = 2;
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetLQData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Pmds_Asp());//沥青病害实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Pmds_Asp"; //沥青病害
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的强度列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISASPHALT");
                    }
                    else if (type == "shas")
                    {
                        startRow = 0;
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetSHASData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Pmds_S());//沥青病害实体
                        exData.DtSource = entity;
                        exData.TableName = "S_PMDS_S"; //沥青病害
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的强度列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISGRAVEL");
                    }
                    else if (type == "sn")
                    {
                        startRow = 1;
                        DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        //验证Excel表头格式?
                        entity = GetSNData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        exData = new Excel_BaseImportData(new S_Pmds_Cem());//水泥病害实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Pmds_Cem"; //水泥病害
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的强度列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISCEMENT");
                    }
                    else if (type == "lj")
                    {
                        startRow = 1;
                        //DataTable dt = Common.C_Excel.ExcelTool.RenderDataTableFromExcel(file, startRow);//获取Excel的DataTable
                        DataTable dt = Common.C_Excel.ExcelTool.GetDataTableFromExcel(file, startRow);
                        if(!old.ContainsKey(type)){
                            old.Add(type,new List<string>());
                        }
                       // entity = GetLJData(dt, rdid, insid, startRow, b_unitList);//将要导入的实体Table
                        entity = GetLJData_New(dt, insid, startRow, old[type]);
                        exData = new Excel_BaseImportData(new S_Subgrd());//水泥病害实体
                        exData.DtSource = entity;
                        exData.TableName = "S_Subgrd"; //水泥病害
                        this.Scene.Bll.Excel.ImportData(exData);
                        //更改评定的路基列
                        this.Scene.Bll.S_Inspeaccse.UpdateStatus(insid, "ISSUB");
                    }
                } 
                catch (Exception ex)
                {
                    File.Delete(file);//删除上传数据
                    //抛出错误
                    var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                    {
                        Content = new StringContent(ex.Message),
                        ReasonPhrase = "Server Error"
                    };
                    throw new HttpResponseException(response);
                }
            }
            else
            {
                //抛出错误
                var response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("服务器未能保存上传文件，请联系管理员"),
                    ReasonPhrase = "Server Error"
                };
                throw new HttpResponseException(response);
            }
            var res = Request.CreateResponse(HttpStatusCode.Created);
            string uri = Url.Link("GetS_InspeaccseByPage", new { page = 1 });
            res.Headers.Location = new Uri(uri);

            return res;
        }

       

        [Route("")]
        public void Delete(S_Inspeaccse entity)
        {
            string insid = entity.Inspecid;//评定ID
            //删除评定管理
            this.Scene.Bll.S_Inspeaccse.Remove(entity);
            //删除抗滑
            this.Scene.Bll.S_Rdfrc.DelByInspecId(insid);
            //删除平整度
            this.Scene.Bll.S_Rdrgh.DelByInspecId(insid);
            //删除路基
            this.Scene.Bll.S_Subgrd.DelByInspecId(insid);
            //删除沿线设施
            this.Scene.Bll.S_Rddf.DelByInspecId(insid);
            //删除构造物
            this.Scene.Bll.S_Btstrut.DelByInspecId(insid);
            //删除车辙
            this.Scene.Bll.S_Rutting.DelByInspecId(insid);
            //删除结构强度
            this.Scene.Bll.S_Rdpssi.DelByInspecId(insid);
            //删除病害(砂石)
            this.Scene.Bll.S_Pmds_S.DelByInspecId(insid);
            //删除病害(沥青)
            this.Scene.Bll.S_Pmds_Asp.DelByInspecId(insid);
            //删除病害(水泥)
            this.Scene.Bll.S_Pmds_Cem.DelByInspecId(insid);
        }


        #region 得到导入的实体的数据
        /// <summary>
        /// 反射获取对象的所有属性
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private PropertyInfo[] GetAllProperty(object t)
        {
            Type temp = t.GetType();
            return temp.GetProperties();
        }

        /// <summary>
        /// 模糊匹配单元，获取起终位置和单元编号
        /// </summary>
        /// <param name="b_unitList"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="unit"></param> 
        private void Matching(List<B_Unit> b_unitList, ref double start, ref double end, ref string unit,ref string name)
        { 
            if(b_unitList.Count > 0)
            {
                foreach(B_Unit b in b_unitList)
                {
                    if(System.Math.Abs(start-b.Beginmp)<500) //默认导入数据以500米为合计一个单元
                    {
                        start = b.Beginmp;
                        end = b.Endmp;
                        unit = b.Unit;
                        name = b.Name;
                        break;
                    }
                }
                if(unit=="")
                {
                    throw new Exception("导入数据未能找到匹配桩号,起终点桩号:" + start+"--" + end);
                }
            }
            else
            {
                throw new Exception("此路线未划分路段,没有桩号");
            }
        }

        /// <summary>
        /// 获取抗滑实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetKHData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Rdfrc());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[0]);//起点桩号
                double _end = Convert.ToDouble(r[1]);//结束桩号
              
                string _rdid = Rdwayid;//路线编号
                string _unit = "";
                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit,ref _name);

                string _svydata = r[2].ToString();//调查日期
                string _device = r[3].ToString();//检测设备
                string _insid = inspecid;//评定ID
                string _surveyer = r[4].ToString();//检测人员
                string _sfc = r[5].ToString();//横向力系数
                string _comments = r[6].ToString();//备注
                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["NAME"] = _name;
                dr["SVYDATA"] = _svydata;
                dr["RDWAYID"] = _rdid;
                dr["DEVICE"] = _device;
                dr["INSPECID"] = _insid;
                dr["SURVEYER"] = _surveyer;
                dr["SFC"] = _sfc;
                dr["COMMENTS"] = _comments;
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取平整度实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetPZDData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Rdrgh());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[0]);//起点桩号
                double _end = Convert.ToDouble(r[1]);//结束桩号
                string _rdid = Rdwayid;//路线编号
                string _unit = "";
                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _svydata = r[2].ToString();//调查日期
                string _device = r[4].ToString();//检测设备
                string _insid = inspecid;//评定ID
                string _surveyer = r[3].ToString();//检测人员
                string _iri = r[5].ToString();//横向力系数
                string _comments = r[6].ToString();//备注
                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["SVYDATA"] = _svydata;
                dr["NAME"] = _name;
                dr["RDWAYID"] = _rdid;
                dr["DEVICE"] = _device;
                dr["INSPECID"] = _insid;
                dr["SURVEYER"] = _surveyer;
                dr["IRI"] = _iri;
                dr["COMMENTS"] = _comments;
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取车辙实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetCZData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {
            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Rutting());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[0]);//起点桩号
                double _end = Convert.ToDouble(r[1]);//结束桩号
                string _rdid = Rdwayid;//路线编号
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _svydata = r[2].ToString();//调查日期
                string _device = r[3].ToString();//检测设备
                string _insid = inspecid;//评定ID
                string _surveyer = r[4].ToString();//检测人员
                string _RUT_LN1 = r[5].ToString();//外道车辙
                string _RUT_LN2 = r[6].ToString();//内道车辙
                string _RD = r[7].ToString();//RD
                string _comments = r[8].ToString();//备注
                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["SVYDATA"] = _svydata;
                dr["NAME"] = _name;
                dr["RDWAYID"] = _rdid;
                dr["DEVICE"] = _device;
                dr["INSPECID"] = _insid;
                dr["SURVEYER"] = _surveyer;
                dr["RD"] = _RD;
                dr["RUT_LN1"] = _RUT_LN1;
                dr["RUT_LN2"] = _RUT_LN2;
                dr["COMMENTS"] = _comments;
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);
            }
            return kh;
        }

        /// <summary>
        /// 获取结构强度实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetQDData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Rdpssi());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[0]);//起点桩号
                double _end = Convert.ToDouble(r[1]);//结束桩号
                
                string _rdid = Rdwayid;//路线编号
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _PSSIDATE = r[2].ToString();//调查日期
                string _device = r[3].ToString();//检测设备
                string _insid = inspecid;//评定ID
                string _surveyer = r[4].ToString();//检测人员
                string _LFDEFL = r[5].ToString();//左弯沉
                string _RGDEFL = r[6].ToString();//右弯沉
                string _REPDEFL = r[7].ToString();//实际弯沉
                double _SSI = 0;//查找SSI
                string _degdf = this.Scene.Bll.B_Pmstc.GetDEGDF(_start, _end, _rdid);//查找设计弯沉
                if (string.IsNullOrEmpty(_degdf))
                {
                    throw new Exception("没有找到桩号对应的设计弯沉,无法计算SSI指标,起点: " + _start + " 终点:" + _end);
                }
                _SSI = Common.C_Utils.Calc.SSI(Convert.ToDouble(_degdf),Convert.ToDouble(_REPDEFL));//得到SSI
                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["PSSIDATE"] = _PSSIDATE;
                dr["RDWAYID"] = _rdid;
                dr["DEVICE"] = _device;
                dr["NAME"] = _name;
                dr["INSPECID"] = _insid;
                dr["SURVEYER"] = _surveyer;
                dr["LFDEFL"] = _LFDEFL;
                dr["RGDEFL"] = _RGDEFL;
                dr["REPDEFL"] = _REPDEFL;
                dr["SSI"] = _SSI;
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取桥隧构造物实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetGZWData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Btstrut());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[0]);//起点桩号
                double _end = Convert.ToDouble(r[1]);//结束桩号
                string _rdid = Rdwayid;//路线编号
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _SVYDATA = r[2].ToString();//调查日期
                string _insid = inspecid;//评定ID
                string _surveyer = r[3].ToString();//检测人员
                string _PMWID = r[4].ToString();//路面宽度
                string _BDTC_OT = r[5].ToString();//桥梁1，2
                string _BDTC_THR = r[6].ToString();//桥梁3
                string _BDTC_FU = r[7].ToString();//桥梁4
                string _BDTC_FV = r[8].ToString();//桥梁5
                string _TUNL_N = r[9].ToString();//隧道技术状况1
                string _TUNL_A = r[10].ToString();//隧道技术状况2
                string _TUNL_D = r[11].ToString();//隧道技术状况3
                string _CLVT_F = r[12].ToString();//涵洞技术状况1
                string _CLVT_LP = r[13].ToString();//涵洞技术状况2
                string _CLVT_P = r[14].ToString();//涵洞技术状况3
                string _CLVT_D = r[15].ToString();//涵洞技术状况4
                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["SVYDATA"] = _SVYDATA;
                dr["NAME"] = _name;
                dr["RDWAYID"] = _rdid;
                dr["INSPECID"] = _insid;
                dr["SVYPS"] = _surveyer;
                dr["PMWID"] = _PMWID;
                dr["BDTC_OT"] = _BDTC_OT;
                dr["BDTC_THR"] = _BDTC_THR;
                dr["BDTC_FU"] = _BDTC_FU;
                dr["BDTC_FV"] = _BDTC_FV;
                dr["TUNL_N"] = _TUNL_N;
                dr["TUNL_A"] = _TUNL_A;
                dr["TUNL_D"] = _TUNL_D;
                dr["CLVT_F"] = _CLVT_F;
                dr["CLVT_LP"] = _CLVT_LP;
                dr["CLVT_P"] = _CLVT_P;
                dr["CLVT_D"] = _CLVT_D;
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取沿线设施实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetSSData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Rddf());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[0]);//起点桩号
                double _end = Convert.ToDouble(r[1]);//结束桩号
             
                string _rdid = Rdwayid;//路线编号
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _SVYDATA = r[2].ToString();//调查日期
                string _insid = inspecid;//评定ID
                string _surveyer = r[3].ToString();//检测人员
                string _PMWID = r[4].ToString();//路面宽度
                string _PTFLDF_L = r[5].ToString();//设施缺损轻
                string _PTFLDF_H = r[6].ToString();//设施缺损重
                string _ISGTDMG = r[7].ToString();//隔离栅损坏
                string _MKDEFT = r[8].ToString();//标志缺损
                string _INDEFT = r[9].ToString();//标线缺损
                string _AFMNGPR = r[10].ToString();//绿化管护               
                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["SVYDATA"] = _SVYDATA;
                dr["RDWAYID"] = _rdid;
                dr["INSPECID"] = _insid;
                dr["NAME"] = _name;
                dr["SVYPS"] = _surveyer;
                dr["PMWID"] = _PMWID;
                dr["PTFLDF_L"] = _PTFLDF_L;
                dr["PTFLDF_H"] = _PTFLDF_H;
                dr["ISGTDMG"] = _ISGTDMG;
                dr["MKDEFT"] = _MKDEFT;
                dr["INDEFT"] = _INDEFT;
                dr["AFMNGPR"] = _AFMNGPR;
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取沥青病害实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetLQData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Pmds_Asp());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[4]);//起点桩号
                double _end = Convert.ToDouble(r[5]);//结束桩号
               
                string _rdid = Rdwayid;//路线编号
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _SVYDATA = r[1].ToString();//调查日期
                string _insid = inspecid;//评定ID
                string _surveyer = r[2].ToString();//检测人员
                string _PMWID = r[3].ToString();//路面宽度
                string _ALGCK_L = r[6].ToString();//龟裂L
                string _ALGCK_M = r[7].ToString();//龟裂M
                string _ALGCK_H = r[8].ToString();//龟裂H
                string _BLFT_L = r[9].ToString();//块裂L
                string _BLFT_H = r[10].ToString();//块裂H
                string _LGCK_L = r[11].ToString();//纵缝L
                string _LGCK_H = r[12].ToString();//纵缝H
                string _TRSCK_L = r[13].ToString();//横缝L
                string _TRSCK_H = r[14].ToString();//横缝H
                string _PIT_L = r[15].ToString();//坑槽L
                string _PIT_H = r[16].ToString();//坑槽H
                string _LOS_L = r[17].ToString();//松散L
                string _LOS_H = r[18].ToString();//松散H
                string _SINK_L = r[19].ToString();//沉陷L
                string _SINK_H = r[20].ToString();//沉陷H
                string _PLWV_L = r[21].ToString();//车辙L
                string _PLWV_H = r[22].ToString();//车辙H
                string _RUT_L = r[23].ToString();//波浪拥包L
                string _RUT_H = r[24].ToString();//波浪拥包H
                string _BLED = r[25].ToString();//泛油
                string _REPAIR = r[26].ToString();//修补
                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["SVYDATA"] = _SVYDATA;
                dr["RDWAYID"] = _rdid;
                dr["INSPECID"] = _insid;
                dr["SVYPS"] = _surveyer;
                dr["NAME"] = _name;
                dr["PMWID"] = _PMWID;
                dr["ALGCK_L"] = _ALGCK_L == "" ? "0" : _ALGCK_L;
                dr["ALGCK_M"] = _ALGCK_M== "" ? "0" :_ALGCK_M ;
                dr["ALGCK_H"] = _ALGCK_H== "" ? "0" :_ALGCK_H ;
                dr["BLFT_L"] = _BLFT_L== "" ? "0" :_BLFT_L ;
                dr["BLFT_H"] = _BLFT_H== "" ? "0" : _BLFT_H;
                dr["LGCK_L"] = _LGCK_L== "" ? "0" : _LGCK_L;
                dr["LGCK_H"] = _LGCK_H== "" ? "0" : _LGCK_H;
                dr["TRSCK_L"] = _TRSCK_L== "" ? "0" :_TRSCK_L ;
                dr["TRSCK_H"] = _TRSCK_H== "" ? "0" :_TRSCK_H ;
                dr["PIT_L"] = _PIT_L== "" ? "0" : _PIT_L;
                dr["PIT_H"] = _PIT_H== "" ? "0" :_PIT_H ;
                dr["LOS_L"] = _LOS_L== "" ? "0" :_LOS_L ;
                dr["LOS_H"] = _LOS_H== "" ? "0" : _LOS_H;
                dr["SINK_L"] = _SINK_L== "" ? "0" :_SINK_L ;
                dr["SINK_H"] = _SINK_H== "" ? "0" :_SINK_H ;
                dr["PLWV_L"] = _PLWV_L== "" ? "0" :_PLWV_L ;
                dr["PLWV_H"] =_PLWV_H == "" ? "0" :_PLWV_H ;
                dr["RUT_L"] = _RUT_L== "" ? "0" :_RUT_L ;
                dr["RUT_H"] = _RUT_H== "" ? "0" :_RUT_H ;
                dr["BLED"] = _BLED== "" ? "0" : _BLED;
                dr["REPAIR"] = _REPAIR == "" ? "0" : _REPAIR;
              
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取砂石病害实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetSHASData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Pmds_S());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[3]);//起点桩号
                double _end = Convert.ToDouble(r[4]);//结束桩号
                
                string _rdid = Rdwayid;//路线编号
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _SVYDATA = r[0].ToString();//调查日期
                string _insid = inspecid;//评定ID
                string _surveyer = r[1].ToString();//检测人员
                string _PMWID = r[2].ToString();//路面宽度

                string _RDCBDC = r[5].ToString();//路拱不适
                string _SINK = r[6].ToString();//沉陷
                string _PLWBD = r[7].ToString();//波浪搓板

                string _RUT = r[8].ToString();//车辙
                string _PIT = r[9].ToString();//坑槽
                string _BRFD = r[10].ToString();//露骨
               

                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["SVYDATA"] = _SVYDATA;
                dr["RDWAYID"] = _rdid;
                dr["NAME"] = _name;
                dr["INSPECID"] = _insid;
                dr["SVYPS"] = _surveyer;
                dr["PMWID"] = _PMWID;
                dr["RDCBDC"] = _RDCBDC == "" ? "0" : _RDCBDC;
                dr["SINK"] = _SINK == "" ? "0" : _SINK;
                dr["PLWBD"] = _PLWBD == "" ? "0" : _PLWBD;
                dr["RUT"] = _RUT == "" ? "0" : _RUT;
                dr["BRFD"] = _BRFD == "" ? "0" : _BRFD;
               
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取水泥病害实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetSNData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {

            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Pmds_Cem());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[3]);//起点桩号
                double _end = Convert.ToDouble(r[4]);//结束桩号
                
                string _rdid = Rdwayid;//路线编号
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);

                string _SVYDATA = r[2].ToString();//调查日期
                string _insid = inspecid;//评定ID
                string _surveyer = r[0].ToString();//检测人员
                string _PMWID = r[1].ToString();//路面宽度

                string _CRSLB_L = r[5].ToString();//破碎板L
                string _CRSLB_H = r[6].ToString();//破碎板H
                string _CRACK_L = r[7].ToString();//裂缝l
                string _CRACK_M = r[8].ToString();//裂缝m
                string _CRACK_H = r[9].ToString();//裂缝h
                string _PLCNCK_L = r[10].ToString();//板角裂缝l
                string _PLCNCK_M = r[11].ToString();//板角裂缝m
                string _PLCNCK_H = r[12].ToString();//板角裂缝h
                string _STGR_L = r[13].ToString();//错台l
                string _STGR_H = r[14].ToString();//错台h
                string _PUMP = r[15].ToString();//唧泥
                string _CNSPL_L = r[16].ToString();//边角剥落l
                string _CNSPL_M = r[17].ToString();//边角剥落m
                string _CNSPL_H = r[18].ToString();//边角剥落h
                string _JNSTDG_L = r[19].ToString();//接缝料l
                string _JNSTDG_H = r[20].ToString();//接缝料h

                string _PTHL = r[21].ToString();//坑洞
                string _HUHUP = r[22].ToString();//拱起
                string _REPAIR = r[23].ToString();//修补


                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit;
                dr["SVYDATA"] = _SVYDATA;
                dr["RDWAYID"] = _rdid;
                dr["INSPECID"] = _insid;
                dr["NAME"] = _name;
                dr["SVYPS"] = _surveyer;
                dr["PMWID"] = _PMWID;
                dr["CRSLB_L"] = _CRSLB_L == "" ? "0" : _CRSLB_L;
                dr["CRSLB_H"] = _CRSLB_H == "" ? "0" : _CRSLB_H;
                dr["CRACK_L"] = _CRACK_L == "" ? "0" : _CRACK_L;
                dr["CRACK_M"] = _CRACK_M == "" ? "0" : _CRACK_M;
                dr["CRACK_H"] = _CRACK_H == "" ? "0" : _CRACK_H;
                dr["PLCNCK_L"] = _PLCNCK_L == "" ? "0" : _PLCNCK_L;
                dr["PLCNCK_M"] = _PLCNCK_M == "" ? "0" : _PLCNCK_M;
                dr["PLCNCK_H"] = _PLCNCK_H == "" ? "0" : _PLCNCK_H;

                dr["STGR_L"] = _STGR_L == "" ? "0" : _STGR_L;
                dr["STGR_H"] = _STGR_H == "" ? "0" : _STGR_H;
                dr["PUMP"] = _PUMP == "" ? "0" : _PUMP;
                dr["CNSPL_L"] = _CNSPL_L == "" ? "0" : _CNSPL_L;
                dr["CNSPL_M"] = _CNSPL_M == "" ? "0" : _CNSPL_M;
                dr["CNSPL_H"] = _CNSPL_H == "" ? "0" : _CNSPL_H;
                dr["JNSTDG_L"] = _JNSTDG_L == "" ? "0" : _JNSTDG_L;
                dr["JNSTDG_H"] = _JNSTDG_H == "" ? "0" : _JNSTDG_H;
                dr["PTHL"] = _PTHL == "" ? "0" : _PTHL;
                dr["HUHUP"] = _HUHUP == "" ? "0" : _HUHUP;
                dr["REPAIR"] = _REPAIR == "" ? "0" : _REPAIR;
                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }
            return kh;
        }

        /// <summary>
        /// 获取路基实体的DataTable
        /// </summary>
        /// <param name="excel">导入excel</param>
        /// <param name="type">导入类型</param>
        /// <param name="Rdwayid">路线编号</param>
        /// <param name="inspecid">评定ID</param>
        /// <param name="dir">调查方向</param>
        /// <returns></returns>
        private DataTable GetLJData(DataTable excel, string Rdwayid, string inspecid, int startRow, List<B_Unit> b_unitList)
        {
            DataTable kh = new DataTable();
            PropertyInfo[] propertys = GetAllProperty(new S_Subgrd());
            foreach (PropertyInfo p in propertys)
            {
                kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
            }
            kh.Columns.Add(new DataColumn("index")); //记录错误行号
            if (excel.Rows.Count == 0)
            {
                throw new Exception("Excel没有数据");
            }
            int index = startRow + 1;
            List<string> test = new List<string>();
            foreach (DataRow r in excel.Rows)
            {
                double _start = Convert.ToDouble(r[0]);//起点桩号
                double _end = Convert.ToDouble(r[1]);//结束桩号
                
                string _rdid = Rdwayid;//路线编号

                //查找单元
                string _unit = "";

                string _name = "";
                //模糊匹配单元
                Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);
                string _SVYDATA = r[2].ToString();//调查日期
                string _insid = inspecid;//评定ID
                string _surveyer = r[3].ToString();//检测人员
                string _PMWID = r[4].ToString();//路面宽度

                string _SLDDHIM = r[5].ToString();//路肩沟不洁
                string _SLDDG_L = r[6].ToString();//路肩损坏l
                string _SLDDG_H = r[7].ToString();//路肩损坏h
                string _SLPCP_L = r[8].ToString();//边坡坍塌l
                string _SLPCP_M = r[9].ToString();//边坡坍塌m
                string _SLPCP_H = r[10].ToString();//边坡坍塌h
                string _WATGUY_L = r[11].ToString();//水毁冲沟l
                string _WATGUY_M = r[12].ToString();//水毁冲沟m
                string _WATGUY_H = r[13].ToString();//水毁冲沟m
                string _DASUGCT_L = r[14].ToString();//路基构造物损坏l
                string _DASUGCT_M = r[15].ToString();//路基构造物损坏m
                string _DASUGCT_H = r[16].ToString();//路基构造物损坏h
                string _CUBDAMG = r[17].ToString();//路缘石损坏
                string _SUBSLT_L = r[18].ToString();//路基沉降l
                string _SUBSLT_M = r[19].ToString();//路基沉降m
                string _SUBSLT_H = r[20].ToString();//路基沉降h

                string _DRSYST_L = r[21].ToString();//排水系统淤塞l
                string _DRSYST_H = r[22].ToString();//排水系统淤塞h



                string _importdate = DateTime.Now.ToShortDateString();//导入时间
                DataRow dr = kh.NewRow();
                dr["UNIT"] = _unit ;
                dr["SVYDATA"] = _SVYDATA;
                dr["RDWAYID"] = _rdid;
                dr["INSPECID"] = _insid;
                dr["SVYPS"] = _surveyer;
                dr["PMWID"] = _PMWID;
                dr["NAME"] = _name;
                dr["SLDDHIM"] = _SLDDHIM == "" ? "0" : _SLDDHIM;
                dr["SLDDG_L"] = _SLDDG_L == "" ? "0" : _SLDDG_L;
                dr["SLDDG_H"] = _SLDDG_H == "" ? "0" : _SLDDG_H;
                dr["SLPCP_L"] = _SLPCP_L == "" ? "0" : _SLPCP_L;
                dr["SLPCP_M"] = _SLPCP_M == "" ? "0" : _SLPCP_M;
                dr["SLPCP_H"] = _SLPCP_H == "" ? "0" : _SLPCP_H;
                dr["WATGUY_L"] = _WATGUY_L == "" ? "0" : _WATGUY_L;
                dr["WATGUY_M"] = _WATGUY_M == "" ? "0" : _WATGUY_M;

                dr["WATGUY_H"] = _WATGUY_H == "" ? "0" : _WATGUY_H;
                dr["DASUGCT_L"] = _DASUGCT_L == "" ? "0" : _DASUGCT_L;
                dr["DASUGCT_M"] = _DASUGCT_M == "" ? "0" : _DASUGCT_M;
                dr["DASUGCT_M"] = _DASUGCT_H == "" ? "0" : _DASUGCT_H;
                dr["CUBDAMG"] = _CUBDAMG == "" ? "0" : _CUBDAMG;
                dr["SUBSLT_L"] = _SUBSLT_L == "" ? "0" : _SUBSLT_L;
                dr["SUBSLT_M"] = _SUBSLT_M == "" ? "0" : _SUBSLT_M;
                dr["SUBSLT_H"] = _SUBSLT_H == "" ? "0" : _SUBSLT_H;
                dr["DRSYST_L"] = _DRSYST_L == "" ? "0" : _DRSYST_L;
                dr["DRSYST_H"] = _DRSYST_H == "" ? "0" : _DRSYST_H;

                dr["IMPORTDATE"] = _importdate;
                dr["index"] = index++;
                kh.Rows.Add(dr);

            }

            return kh;
        }

       


        #endregion
        
        /// <summary>
        /// 获取所有评测信息
        /// </summary>
        /// <returns></returns>
        [Route("all")]
        public dynamic GetAllInspeaccse()
        {
            return this.Scene.Bll.S_Inspeaccse.Get<S_Inspeaccse>(null, null);
        }

        #region 新的处理逻辑
        /// <summary>
        /// 检测起终点桩号是否重复
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="start">起点索引</param>
        /// <param name="end">终点索引</param>
         private void  CheckDatatable(DataTable dt,int start,int end)
         {
             //List<int> samerow=new List<int>();
             //int flag = 0;
             //List<DataRow> listdr = new List<DataRow>();
             for (int i = 0; i < dt.Rows.Count; i++)
             {
                 DataRow dr = dt.Rows[i];//第1行
                 for (int j = i+1; j < dt.Rows.Count; j++)
                 {
                     DataRow secdr = dt.Rows[j]; //第2行
                     if (dr[start].ToString() == secdr[start].ToString() || dr[end].ToString() == secdr[end].ToString())
                     {
                         throw new Exception("起点或终点桩号重复");
                     }
                     //for (int k = 0; k < dt.Columns.Count; k++)
                     //{
                     //    if (dr[k].ToString() == secdr[k].ToString())
                     //        flag++;
                     //    if (flag == dt.Columns.Count)
                     //        samerow.Add(j);
                     //}
                     //flag = 0;
                 }
             }
             //IEnumerable<int> sameindex = samerow.Distinct();
             //foreach (var item in sameindex)
             //{
             //    listdr.Add(dt.Rows[item]);//记录dt中重复行
             //}
             //foreach (var item in listdr)
             //{
             //    dt.Rows.Remove(item);//移除dt中重复行
             //}
             //return dt;
         }

         private DataTable GetLJData_New(DataTable excel, string inspecid,int startRow, List<string> oldUnit)
         {
             DataTable kh = new DataTable();
             PropertyInfo[] propertys = GetAllProperty(new S_Subgrd()); //路基
             foreach (PropertyInfo p in propertys)
             {
                 kh.Columns.Add(new DataColumn(p.Name.ToUpper()));
             }
             kh.Columns.Add(new DataColumn("index")); //记录错误行号
             if (excel.Rows.Count == 0)
             {
                 throw new Exception("Excel没有数据");
             }
             int index = startRow + 1;

             Dictionary<string, List<B_Unit>> temp = new Dictionary<string, List<B_Unit>>();
             CheckDatatable(excel, 1, 2);
             //遍历DataTable(路线,起终桩号等数据,方向)
             foreach (DataRow r in excel.Rows)
             {
                 //查询该线路该方向的的所有单元
                 double _start = Convert.ToDouble(r[1]);//起点桩号
                 double _end = Convert.ToDouble(r[2]);//结束桩号

                 string _rdid = r[0].ToString();//路线编号
                 string _dir = r[excel.Columns.Count - 1].ToString();//方向  
                 //该线路该方向下的所有单元
                 List<B_Unit> b_unitList = null;
                 if (!temp.ContainsKey(_rdid + "_" + _dir))
                 {
                      b_unitList = this.Scene.Bll.B_Unit.GetUnitList<B_Unit>(_dir, _rdid);
                     temp.Add(_rdid + "_" + _dir, b_unitList);
                 }
                 //查找单元
                 string _unit = "";
                 string _name = "";
                 //模糊匹配单元
                 Matching(b_unitList, ref _start, ref _end, ref _unit, ref _name);
                 //查询该单元是否已经有今年的评定数据
                 string _SVYDATA = r[3].ToString();//调查日期
                 string _insid = inspecid;//评定ID
                 string _surveyer = r[4].ToString();//检测人员
                 string _PMWID = r[5].ToString();//路面宽度

                 string _SLDDHIM = r[6].ToString();//路肩沟不洁
                 string _SLDDG_L = r[7].ToString();//路肩损坏l
                 string _SLDDG_H = r[8].ToString();//路肩损坏h
                 string _SLPCP_L = r[9].ToString();//边坡坍塌l
                 string _SLPCP_M = r[10].ToString();//边坡坍塌m
                 string _SLPCP_H = r[11].ToString();//边坡坍塌h
                 string _WATGUY_L = r[12].ToString();//水毁冲沟l
                 string _WATGUY_M = r[13].ToString();//水毁冲沟m
                 string _WATGUY_H = r[14].ToString();//水毁冲沟m
                 string _DASUGCT_L = r[15].ToString();//路基构造物损坏l
                 string _DASUGCT_M = r[16].ToString();//路基构造物损坏m
                 string _DASUGCT_H = r[17].ToString();//路基构造物损坏h
                 string _CUBDAMG = r[18].ToString();//路缘石损坏
                 string _SUBSLT_L = r[19].ToString();//路基沉降l
                 string _SUBSLT_M = r[20].ToString();//路基沉降m
                 string _SUBSLT_H = r[21].ToString();//路基沉降h
                 string _DRSYST_L = r[22].ToString();//排水系统淤塞l
                 string _DRSYST_H = r[23].ToString();//排水系统淤塞h
                 string _importdate = DateTime.Now.ToShortDateString();//导入时间

                 DataRow dr = kh.NewRow();
                 dr["UNIT"] = _unit;
                 dr["SVYDATA"] = _SVYDATA;
                 dr["RDWAYID"] = _rdid;
                 dr["NAME"] = _name;
                 dr["INSPECID"] = _insid;
                 dr["SVYPS"] = _surveyer;
                 dr["PMWID"] = _PMWID;
                 dr["SLDDHIM"] = _SLDDHIM == "" ? "0" : _SLDDHIM;
                 dr["SLDDG_L"] = _SLDDG_L == "" ? "0" : _SLDDG_L;
                 dr["SLDDG_H"] = _SLDDG_H == "" ? "0" : _SLDDG_H;
                 dr["SLPCP_L"] = _SLPCP_L == "" ? "0" : _SLPCP_L;
                 dr["SLPCP_M"] = _SLPCP_M == "" ? "0" : _SLPCP_M;
                 dr["SLPCP_H"] = _SLPCP_H == "" ? "0" : _SLPCP_H;
                 dr["WATGUY_L"] = _WATGUY_L == "" ? "0" : _WATGUY_L;
                 dr["WATGUY_M"] = _WATGUY_M == "" ? "0" : _WATGUY_M;
                 dr["WATGUY_H"] = _WATGUY_H == "" ? "0" : _WATGUY_H;
                 dr["DASUGCT_L"] = _DASUGCT_L == "" ? "0" : _DASUGCT_L;
                 dr["DASUGCT_M"] = _DASUGCT_M == "" ? "0" : _DASUGCT_M;
                 dr["DASUGCT_M"] = _DASUGCT_H == "" ? "0" : _DASUGCT_H;
                 dr["CUBDAMG"] = _CUBDAMG == "" ? "0" : _CUBDAMG;
                 dr["SUBSLT_L"] = _SUBSLT_L == "" ? "0" : _SUBSLT_L;
                 dr["SUBSLT_M"] = _SUBSLT_M == "" ? "0" : _SUBSLT_M;
                 dr["SUBSLT_H"] = _SUBSLT_H == "" ? "0" : _SUBSLT_H;
                 dr["DRSYST_L"] = _DRSYST_L == "" ? "0" : _DRSYST_L;
                 dr["DRSYST_H"] = _DRSYST_H == "" ? "0" : _DRSYST_H;
                 dr["IMPORTDATE"] = _importdate;
                 dr["index"] = index++;
                 kh.Rows.Add(dr);

                 //检查是否有该批次最新的重复评定数据
                 string oldSVYDATA = this.Scene.Bll.S_Subgrd.Exists(_unit, inspecid);
                 //如果存在
                 if(oldSVYDATA!="")
                 {
                     oldUnit.Add(_unit+"|"+oldSVYDATA);//保存重复值
                 }
             }
             return kh;
         }


        #endregion


        [Route("inspecid")]
        public dynamic GetInspecid(string rdwayid)
        {
            var innerJoinQuery =
                from road in this.Scene.Bll.S_Inspeaccse.List(rdwayid)
                select new { id = road, text = road };
            return innerJoinQuery;

        }


        /// <summary>
        /// 上传返回上传后路径
        /// </summary>
        [Route("uploadFile")]
        public void PostUploadFile()
        {

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            string uploadPath =
              System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Upload") + "\\";
            string PhysicalPath = "";
            if (hfc.Count > 0)
            {
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                PhysicalPath = uploadPath + hfc[0].FileName;
                hfc[0].SaveAs(PhysicalPath);
            }

            System.Web.HttpContext.Current.Response.Write(PhysicalPath);
            System.Web.HttpContext.Current.Response.End();
        }
    }
}